package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.enums.DictEnum;
import com.atguigu.yygh.hosp.repository.HospitalMongoRepository;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/*==========================================
      时 间:   2022-04-18
      讲 师:   刘  辉
      描 述:   尚硅谷教学团队
 ==========================================*/
@Service
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    private HospitalMongoRepository hospitalMongoRepository;
    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> stringObjectMap) {
        Hospital hospital = JSONObject.parseObject(JSONObject.toJSONString(stringObjectMap), Hospital.class);

        Hospital queryHospital=hospitalMongoRepository.findByHoscode(hospital.getHoscode());

        if(queryHospital == null){//如果不存在，做添加
            //0：未上线 1：已上线
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalMongoRepository.save(hospital);
        }else{//如果已存在，做修改
            hospital.setStatus(queryHospital.getStatus());
            hospital.setCreateTime(queryHospital.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(queryHospital.getIsDeleted());
            hospital.setId(queryHospital.getId());
            hospitalMongoRepository.save(hospital);
        }


    }

    @Override
    public Hospital getHosptialInfo(String hoscode) {
        return hospitalMongoRepository.findByHoscode(hoscode);
    }


    @Override
    public Page<Hospital> getHospitalPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {

        Hospital hospital=new Hospital();
        hospital.setHosname(hospitalQueryVo.getHosname());
        hospital.setProvinceCode(hospitalQueryVo.getProvinceCode());
        hospital.setCityCode(hospitalQueryVo.getCityCode());

        hospital.setHostype(hospitalQueryVo.getHostype());
        hospital.setDistrictCode(hospitalQueryVo.getDistrictCode());

        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withMatcher("hosname",ExampleMatcher.GenericPropertyMatchers.contains()) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写

        Example<Hospital> example=Example.of(hospital,matcher);

        PageRequest pageRequest = PageRequest.of(page - 1, limit, Sort.by(Sort.Direction.ASC, "createTime"));
        Page<Hospital> pageResult = hospitalMongoRepository.findAll(example, pageRequest);

        pageResult.getContent().parallelStream().forEach(item->{
            this.packageHospital(item);
        });
        return pageResult;
    }

    @Override
    public void updateStatus(String id, Integer status) {
        if(status == 1 || status == 0){
            Hospital hospital = hospitalMongoRepository.findById(id).get();
            hospital.setStatus(status);
            hospital.setUpdateTime(new Date());
            hospitalMongoRepository.save(hospital);
        }
    }

    @Override
    public Hospital detail(String id) {
        Hospital hospital = hospitalMongoRepository.findById(id).get();
        packageHospital(hospital);
        return hospital;
    }

    @Override
    public List<Hospital> findByHosnameLike(String name) {
       return hospitalMongoRepository.findByHosnameLike(name);
    }

    @Override
    public Hospital getPackageHosptialInfo(String hoscode) {
        Hospital hospital = hospitalMongoRepository.findByHoscode(hoscode);
        this.packageHospital(hospital);
        return hospital;
    }

    private void packageHospital(Hospital item) {
        String provinceName = dictFeignClient.getNameByValue(item.getProvinceCode());
        String cityName = dictFeignClient.getNameByValue(item.getCityCode());
        String districtName = dictFeignClient.getNameByValue(item.getDistrictCode());
        String hospitalLevel = dictFeignClient.getNameByDictCodeAndValue(DictEnum.HOSTYPE.getDictCode(), item.getHostype());

        item.getParam().put("hostypeString", hospitalLevel);
        item.getParam().put("fullAddress", provinceName + cityName + districtName + item.getAddress());
    }


}
